//
//  LifeToolsViewController.m
//  KL
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

#import "LifeToolsViewController.h"
#import "KLKuaidiViewController.h"
#import <MJExtension.h>
#import "KLLifeTools.h"
#import "NavViewController.h"
#import "KLSettingTableViewController.h"


@interface LifeToolsViewController ()

/** 模型数组 */
@property (nonatomic, strong) NSArray *lifeTools;

@end

@implementation LifeToolsViewController

#pragma mark - 重写init方法
- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置右上角的item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar_setting"] style:UIBarButtonItemStylePlain target:self action:@selector(pushSetting)];
}

#pragma mark - 进入设置页面
- (void)pushSetting {
    KLSettingTableViewController *setting = [[KLSettingTableViewController alloc] init];
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.lifeTools.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.lifeTools[section][@"item"];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    // 取出每组的模型
    NSArray *array = self.lifeTools[indexPath.section][@"item"];
    NSDictionary *dict = array[indexPath.row];
    
    cell.textLabel.text = dict[@"name"];
    cell.imageView.image = [UIImage imageNamed:dict[@"icon"]];
    cell.detailTextLabel.text = dict[@"subTitle"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    return cell;
}

// 每组的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return self.lifeTools[section][@"headerTitle"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 取消选中状态
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    // 取出点击的cell模型
    NSArray *array = self.lifeTools[indexPath.section][@"item"];
    NSDictionary *dict = array[indexPath.row];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:dict[@"sbName"] bundle:nil];
    UIViewController *VC = [sb instantiateViewControllerWithIdentifier:dict[@"destiVC"]];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 懒加载
- (NSArray *)lifeTools {
    if (!_lifeTools) {
        _lifeTools = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lifeTools.plist" ofType:nil]];
    }
    return _lifeTools;
}

@end
